import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-siteCode2-page',
	templateUrl: './siteCode2-page.component.html',
})
export class SiteCode2PageComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			stationName: {
				validators: {
					notEmpty: {
						message: '请输入站点名称'
					}
				}
			},
			areaName: {
				validators: {
					notEmpty: {
						message: '请输入区域名称'
					}
				}
			},
			longitude: {
				validators: {
					callback:{
	                  	callback:function(value,validator,$field){
	                  		if(value){
	                  			if(value.length<17){
		                  			var reg = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,14})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,14}|180)$/;
		                  			if(!reg.test(value)){
		                  				return{
			                  				valid:false,
			                  				message:'经度有误'
			                  			}
			                  		}else{
			                  			return true;
			                  		}
		                  		}else{
		                  			return{
		                  				valid:false,
		                  				message:'长度控制在17以内'
		                  			}
		                  		}
	                  		}else{
	                  			return true;
	                  		}
	                  	}
	              	},
				}
			},
			latitude: {
				validators: {
					callback:{
						callback:function(value,validator,$field){
							if(value){
								if(value.length<17){
	                  				var reg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,14}|90\.0{0,14}|[0-8]?\d{1}|90)$/;
		                  			if(!reg.test(value)){
		                  				return{
			                  				valid:false,
			                  				message:'纬度有误'
			                  			}
			                  		}else{
			                  			return true;
			                  		}
		                  		}else{
		                  			return{
		                  				valid:false,
		                  				message:'长度控制在17以内'
		                  			}
		                  		}
							}else{
								return true;
							}
	                  	}						
	              	},
				}
			},

		}

	}
	page: any = {
		id:"",
		stationName:"",
		areaName: "",
		longitude: "",
		lantitude:"",
		stationAddress: ""
	};
	page_title:string = '新增';
	pageType:string = 'new';
	siteId = null;
	timer:any;
	curPage:number = 0;

	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.siteId = queryParams.siteId;
			this.pageType = queryParams.type;
			this.curPage = queryParams.nowPage;
		});
	}
	//提交校验
	onSubmit() {
		if(this.pageType === 'new'){
			$('#lySiteForm').bootstrapValidator('validate'); //开启验证
			let isValid = $('#lySiteForm').data('bootstrapValidator').isValid();  //验证是否通过
			if(isValid){
				this.updata();
			}
		}else if(this.pageType === 'updata'){
			$('#lySiteForm').bootstrapValidator('validate'); //开启验证
			this.timer = setTimeout(()=>{
				let isValid = $('#lySiteForm').data('bootstrapValidator').isValid();  //验证是否通过
				if(isValid){
					this.updata();
				}
			},200)
		}
	}
	//表单提交
	updata(){
		clearTimeout(this.timer);
		var reqdata,url;
		var params:any={
			stationName:this.page.stationName,
			areaName: this.page.areaName,
			longitude: this.page.longitude,
			lantitude:this.page.lantitude,
			stationAddress: this.page.stationAddress,
		}
		if(this.pageType === 'new'){
			url = 'QRcodeStationManage/insertQRcodeStationManage.html';
		}else if(this.pageType === 'updata'){
			params.id=this.siteId;
			url = 'QRcodeStationManage/updateQRcodeStationManage.html';
		}
		reqdata = 'params=' + JSON.stringify(params);
		this.jsonApiService.http(url,reqdata).subscribe(data=> {
			this.jsonApiService.message(data.message);
			this.goBack();
		})
	}
	//返回资源列表页
	goBack() {
		this.router.navigate(['/langyun/lySystem/siteCode2'],{ queryParams: {nowPage:this.curPage}});
	}
	ngOnInit() {
		if(this.pageType === 'updata') {
			this.page_title = '编辑';
			let reqdata = 'id='+this.siteId;
			this.jsonApiService.http("QRcodeStationManage/editQRcodeStationManage.html",reqdata).subscribe(data=> {
				if(data.success){
					for(let item in this.page) {
						this.page[item] = data.data[item];
					}
				}else{
					this.jsonApiService.message(data.message);
				}
				
			})
		} else {
			return
		}
	}

}